package com.jlnk.stockdemo.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class SPUtils {
    // SharedPreferences文件名
    private static final String SP_NAME = "login_info";
    // SharedPreferences对象
    private static SharedPreferences sp;

    // 初始化方法，传入Context和文件名
    public static void init(Context context) {
        sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
    }

    // 保存账号
    public static void saveUsername(String username) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("username", username);
        editor.apply();
    }

    // 获取账号
    public static String getUsername() {
        return sp.getString("username", "");
    }

    // 保存密码
    public static void savePassword(String password) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("password", password);
        editor.apply();
    }

    // 获取密码
    public static String getPassword() {
        return sp.getString("password", "");
    }

    // 保存头像路径
    public static void saveAvatorFilePath(String avatorFilePath) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("avatorFilePath", avatorFilePath);
        editor.apply();
    }

    // 获取头像路径
    public static String getAvatorFilePath() {
        return sp.getString("avatorFilePath", "");
    }

    // 清除账号和密码
    public static void clear() {
        SharedPreferences.Editor editor = sp.edit();
        editor.remove("username");
        editor.remove("password");
        editor.remove("avatorFilePath");
        editor.apply();
    }
}
